import 'dart:io';

import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/services.dart';

/**
 * @Description: 包名信息工具类
 * @Author: 下雨了
 * @CreateDate: 2021-12-15 15:59
 * @UpdateUser: 更新者
 * @UpdateDate:  2021-12-15 15:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
///
//定义一个top-level（全局）变量，页面引入该文件后可以直接使用 PackageInfoUtils
// final deviceInfo = DeviceInfoUtils();

class DeviceInfoUtils {
  // 私有构造函数
  DeviceInfoUtils._internal();

  // 保存单例
  static final DeviceInfoUtils _deviceInfoUtils = DeviceInfoUtils._internal();

  // 工厂构造函数
  factory DeviceInfoUtils() => _deviceInfoUtils;

  DeviceInfoPlugin initDeviceInfo() {
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    return deviceInfo;
  }

  Future<AndroidDeviceInfo> getAndroidDeviceInfo() async {
    return await initDeviceInfo().androidInfo;
  }

  Future<IosDeviceInfo> getIosDeviceInfo() async {
    return await initDeviceInfo().iosInfo;
  }

  Future<WebBrowserInfo> getWebBrowserInfo() async {
    return await initDeviceInfo().webBrowserInfo;
  }

  /// android 获取 androidID  ios获取UUID
  Future<String?> get getDeviceId async {
    String? deviceId;
    try {
      if (Platform.isAndroid) {
        deviceId = await getAndroidDeviceInfo().then((value) => value.androidId);
      } else if (Platform.isIOS) {
        deviceId = await getIosDeviceInfo().then((value) => value.identifierForVendor);
      }
    } on PlatformException {
      deviceId = '';
    }
    return deviceId;
  }

  /// android 获取androidInfo brand  model    ios获取iosInfo name
  Future<String?> get getDeviceName async {
    String? deviceId;
    try {
      if (Platform.isAndroid) {
        deviceId = await getAndroidDeviceInfo().then((value) =>  "${value.brand} ${value.model}");
      } else if (Platform.isIOS) {
        deviceId = await getIosDeviceInfo().then((value) => value.name);
      }
    } on PlatformException {
      deviceId = '';
    }
    return deviceId;
  }

  /// android 获取androidInfo version.release    ios获取iosInfo systemVersion
  Future<String?> get getDeviceSys async {
    String? deviceId;
    try {
      if (Platform.isAndroid) {
        deviceId = await getAndroidDeviceInfo().then((value) => "Android ${value.version.release}");
      } else if (Platform.isIOS) {
        deviceId = await getIosDeviceInfo().then((value) => "${value.systemName} ${value.systemVersion}");
      }
    } on PlatformException {
      deviceId = '';
    }
    return deviceId;
  }

  ///获取设备信息
  static get deviceInfo async {
    final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
    Map<String, dynamic> deviceData = <String, dynamic>{};
    AndroidDeviceInfo? androidInfo;
    IosDeviceInfo? iosInfo;
    if (Platform.isIOS) {
      iosInfo = await deviceInfoPlugin.iosInfo;
    } else {
      androidInfo = await deviceInfoPlugin.androidInfo;
    }
    deviceData = _readDeviceInfo(androidInfo, iosInfo);
    return deviceData;
  }

  static _readDeviceInfo(
      AndroidDeviceInfo? androidInfo, IosDeviceInfo? iosInfo) {
    Map<String, dynamic> data = <String, dynamic>{
      //手机品牌加型号
      "brand": Platform.isIOS
          ? iosInfo?.name
          : "${androidInfo?.brand} ${androidInfo?.model}",
      //当前系统版本
      "systemVersion": Platform.isIOS
          ? iosInfo?.systemVersion
          : androidInfo?.version.release,
      //系统名称
      "Platform": Platform.isIOS ? iosInfo?.systemName : "Android",
      //是不是物理设备
      "isPhysicalDevice": Platform.isIOS
          ? iosInfo?.isPhysicalDevice
          : androidInfo?.isPhysicalDevice,
      //用户唯一识别码
      "uuid": Platform.isIOS
          ? iosInfo?.identifierForVendor
          : androidInfo?.androidId,
      //手机具体的固件型号/Ui版本
      "incremental": Platform.isIOS
          ? iosInfo?.systemVersion
          : androidInfo?.version.incremental,
    };
    return data;
  }
}

// {
  //   "id": "HUAWEIHMA-AL00",
  //   "host": "szvjk004cna",
  //   "tags": "release-keys",
  //   "type": "user",
  //   "model": "HMA-AL00",
  //   "board": "HMA",
  //   "brand": "HUAWEI",
  //   "device": "HWHMA",
  //   "product": "HMA-AL00",
  //   "display": "HMA-AL00 9.0.0.205(C00E205R1P21)",
  //   "hardware": "kirin980",
  //   "androidId": "5e36aade2c2f8674",
  //   "bootloader": "unknown",
  //   "version": {
  //     "baseOS": "",
  //     "sdkInt": 28,
  //     "release": "9",
  //     "codename": "REL",
  //     "incremental": "205C00",
  //     "previewSdkInt": 0,
  //     "securityPatch": "2019-05-05"
  //   },
  //   "fingerprint": "HUAWEI/HMA-AL00/HWHMA:9/HUAWEIHMA-AL00/205C00:user/release-keys",
  //   "manufacturer": "HUAWEI",
  //   "supportedAbis": [
  //     "arm64-v8a",
  //     "armeabi-v7a",
  //     "armeabi"
  //   ],
  //   "systemFeatures": [
  //     "android.hardware.sensor.proximity",
  //     "android.hardware.sensor.accelerometer",
  //     "android.hardware.faketouch",
  //     "android.hardware.usb.accessory",
  //     "android.hardware.telephony.cdma",
  //     "android.software.backup",
  //     "android.hardware.touchscreen",
  //     "android.hardware.touchscreen.multitouch",
  //     "android.software.print",
  //     "android.hardware.consumerir",
  //     "android.software.activities_on_secondary_displays",
  //     "android.software.voice_recognizers",
  //     "android.software.picture_in_picture",
  //     "android.hardware.fingerprint",
  //     "android.hardware.sensor.gyroscope",
  //     "android.hardware.audio.low_latency",
  //     "android.software.cant_save_state",
  //     "android.hardware.opengles.aep",
  //     "android.hardware.bluetooth",
  //     "android.hardware.camera.autofocus",
  //     "android.hardware.telephony.gsm",
  //     "android.hardware.usb.host",
  //     "android.hardware.audio.output",
  //     "com.google.android.feature.services_updater",
  //     "android.software.verified_boot",
  //     "android.hardware.camera.flash",
  //     "android.hardware.camera.front",
  //     "android.hardware.screen.portrait",
  //     "android.hardware.nfc",
  //     "cn.google.services",
  //     "android.hardware.sensor.stepdetector",
  //     "android.hardware.microphone",
  //     "android.software.autofill",
  //     "android.software.securely_removes_users",
  //     "android.hardware.bluetooth_le",
  //     "android.hardware.sensor.compass",
  //     "android.hardware.touchscreen.multitouch.jazzhand",
  //     "android.hardware.sensor.barometer",
  //     "android.software.app_widgets",
  //     "android.software.input_methods",
  //     "android.hardware.sensor.light",
  //     "android.hardware.vulkan.version",
  //     "android.software.companion_device_setup",
  //     "android.software.device_admin",
  //     "android.hardware.camera",
  //     "android.hardware.screen.landscape",
  //     "android.hardware.ram.normal",
  //     "android.software.managed_users",
  //     "android.software.webview",
  //     "android.hardware.sensor.stepcounter",
  //     "android.hardware.camera.capability.manual_post_processing",
  //     "android.hardware.camera.any",
  //     "android.hardware.camera.capability.raw",
  //     "android.software.connectionservice",
  //     "android.hardware.touchscreen.multitouch.distinct",
  //     "android.hardware.location.network",
  //     "android.software.cts",
  //     "android.hardware.wifi.direct",
  //     "android.software.live_wallpaper",
  //     "android.hardware.nfc.hcef",
  //     "android.hardware.location.gps",
  //     "android.software.midi",
  //     "android.hardware.nfc.any",
  //     "android.hardware.nfc.hce",
  //     "android.hardware.wifi",
  //     "android.hardware.location",
  //     "android.hardware.vulkan.level",
  //     "android.hardware.telephony",
  //     "android.software.file_based_encryption",
  //     null
  //   ],
  //   "isPhysicalDevice": true,
  //   "supported32BitAbis": [
  //     "armeabi-v7a",
  //     "armeabi"
  //   ],
  //   "supported64BitAbis": [
  //     "arm64-v8a"
  //   ]
  // }
